package daily.oct;

public class Bank {
    // leetcode 2043题
    /*非常简单，几乎没遇到问题。
    但是要注意题目在银行只有1个账户时，允许自己向自己转账（从account1转到account1），所以注释了第19行代码
    */

    public long[] balance;
    public Bank(long[] balance) {
        this.balance = balance;
    }



    public boolean transfer(int account1, int account2, long money) {
        int len = balance.length;
        //if (len<2) return false;//账户数量不足
        if (account1>len || account2>len) return false;//不存在account1或account2,越界

        //如果是2号账户，在balance中的下标为1
        if (balance[account1-1]>=money) {
            balance[account1-1]=balance[account1-1]-money;
            balance[account2-1]=balance[account2-1]+money;
            return true;
        }else  {
            return false;
        }
    }

    public boolean deposit(int account, long money) {//存钱
        if (account>balance.length) return false;

        balance[account-1]=balance[account-1]+money;
        return true;
    }

    public boolean withdraw(int account, long money) {//取钱
        if (account>balance.length) return false;

        if (balance[account-1]<money) {
            return  false;
        }else  {
            balance[account-1]=balance[account-1]-money;
            return true;
        }
    }

    public static void main(String[] args) {
        Bank bank = new Bank(new long[]{10,100,20,50,30});
        System.out.println(bank.withdraw(3,10));
        System.out.println(bank.transfer(5,1,20));
        System.out.println(bank.deposit(5,20));
        System.out.println(bank.transfer(3,4,15));
        System.out.println(bank.withdraw(10,50));
    }

}
